package com.smartwasser.yunhtgl.apphtgl.asp;

import android.app.Dialog;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.adapter.SPAdapter01;
import com.smartwasser.yunhtgl.apphtgl.asps.SPCase4Activity;
import com.smartwasser.yunhtgl.apphtgl.asps.SPGreat4Activity;
import com.smartwasser.yunhtgl.apphtgl.asps.SPNormal4Activity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseSpinner;
import com.smartwasser.yunhtgl.apphtgl.bean.SP1Bean;
import com.smartwasser.yunhtgl.apphtgl.presenter.SP01Presenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.SP01PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.view.SP01View;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils;
import com.smartwasser.yunhtgl.apphtgl.widget.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.view.View.inflate;
import static com.dou361.dialogui.DialogUIUtils.dismiss;

/**
 * 已终止合同
 */
public class SP04Activity extends BaseActivity implements SP01View {
    private SP01Presenter sp0001Presenter = new SP01PresenterImpl(this);
    private Dialog loadingHorizontal;
    private SPAdapter01 mAdapter;
    private int total = 0;
    private int page = 1;
    private List<SP1Bean.PageBean.ListBean> mList = new ArrayList();
    private String category = "", procurementMode = "", moneyRange = "";
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.sp_list)
    ListView sp_list;
    @BindView(R.id.spinner1)
    Spinner spinner1;
    @BindView(R.id.spinner2)
    Spinner spinner2;
    @BindView(R.id.spinner3)
    Spinner spinner3;
    @BindView(R.id.swipeRefresh)
    RefreshLayout swipeRefresh;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_sp01);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("已中止合同");
        initRecyclerView();
        initSpinner();
    }

    @Override
    public void initData() {
        loadingHorizontal = showLoadingHorizontal(this, "正在加载...").show();
        getMessagesData();
    }

    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    public void getMessagesData() {
        try {
            sp0001Presenter.getType1SP04(SharedUtils.getStringData(this, "JSESSIONID", ""), page, category, procurementMode, moneyRange);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void success(SP1Bean sp0001Bean) {
        dismiss(loadingHorizontal);
        if (swipeRefresh.getLoading()) {
            swipeRefresh.setLoading(false);
            mAdapter.notifyDataSetChanged();
        }
        total = sp0001Bean.getPage().getCount();
        if (sp0001Bean.getPage().getList().size() > 0) {
            if (null != mList && mList.size() > 0 && !(mList.contains(sp0001Bean.getPage().getList()))) {
                mList.addAll(sp0001Bean.getPage().getList());
                mAdapter.refresh(mList);
            } else if (null == mList || mList.size() == 0) {
                mList = sp0001Bean.getPage().getList();
                mAdapter.refresh(mList);
            }
        }
    }

    @Override
    public void error() {
        dismiss(loadingHorizontal);
        showError();
        if (swipeRefresh.getLoading()) {
            swipeRefresh.setLoading(false);
            mAdapter.notifyDataSetChanged();
        }
    }

    private void loadData() {
        Logger.e("total = " + total);
        Logger.e("mList = " + mList.size());
        if (total > mList.size()) {
            page++;
            if (swipeRefresh.pull_to_refresh_loadmore_text.getText().equals("没有更多数据")) {
                swipeRefresh.pull_to_refresh_loadmore_text.setText("正在加载...");
            }
            swipeRefresh.pull_to_refresh_load_progress.setVisibility(View.VISIBLE);
            getMessagesData();
        } else {
            swipeRefresh.pull_to_refresh_loadmore_text.setText("没有更多数据");
            swipeRefresh.pull_to_refresh_load_progress.setVisibility(View.GONE);
            mAdapter.notifyDataSetChanged();
        }
        Logger.e("page = " + page);
    }


    private void initRecyclerView() {
        mAdapter = new SPAdapter01(this, mList);
        sp_list.setAdapter(mAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        mList.clear();
                        mAdapter.notifyDataSetChanged();
                        getMessagesData();
                        swipeRefresh.setRefreshing(false);
                    }
                }, 500);
            }
        });
        swipeRefresh.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                mAdapter.notifyDataSetChanged();
                swipeRefresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadData();
                    }
                }, 500);
            }
        });
        sp_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (mList.get(position).getCategory().equals("0")) {
                    startActivity(SPGreat4Activity.class, "id", mList.get(position).getId());
                } else if (mList.get(position).getCategory().equals("1")) {
                    startActivity(SPNormal4Activity.class, "id", mList.get(position).getId());
                } else if (mList.get(position).getCategory().equals("2")) {
                    startActivity(SPCase4Activity.class, "id", mList.get(position).getId());
                }
            }
        });
    }

    private void initSpinner() {
        final List<String> arrayList1 = new ArrayList<>();
        arrayList1.add("全部合同");
        arrayList1.add("重大合同");
        arrayList1.add("一般合同");
        arrayList1.add("合同用印");
        final List<String> arrayList2 = new ArrayList<>();
        arrayList2.add("全部方式");
        arrayList2.add("集采");
        arrayList2.add("非集采");
        final List<String> arrayList3 = new ArrayList<>();
        arrayList3.add("全部金额");
        arrayList3.add("0 ~ 40");
        arrayList3.add("40 ~ 100");
        arrayList3.add("100以上");

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                R.layout.spinner_drop, arrayList1) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View view = inflate(getContext(), R.layout.spinner_item_layout, null);
                TextView label = view.findViewById(R.id.spinner_item_label);
                label.setText(arrayList1.get(position));
                if (spinner1.getSelectedItemPosition() == position) {
                    label.setTextColor(getResources().getColor(R.color.colorTitle));
                } else {
                    label.setTextColor(getResources().getColor(R.color.colorText));
                }

                return view;
            }
        };
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                R.layout.spinner_drop, arrayList2) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View view = inflate(getContext(), R.layout.spinner_item_layout, null);
                TextView label = view.findViewById(R.id.spinner_item_label);
                label.setText(arrayList2.get(position));
                if (spinner2.getSelectedItemPosition() == position) {
                    label.setTextColor(getResources().getColor(R.color.colorTitle));
                } else {
                    label.setTextColor(getResources().getColor(R.color.colorText));
                }
                return view;
            }

        };
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,
                R.layout.spinner_drop, arrayList3) {

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View view = inflate(getContext(), R.layout.spinner_item_layout, null);
                TextView label = view.findViewById(R.id.spinner_item_label);
                label.setText(arrayList3.get(position));
                if (spinner3.getSelectedItemPosition() == position) {
                    label.setTextColor(getResources().getColor(R.color.colorTitle));
                } else {
                    label.setTextColor(getResources().getColor(R.color.colorText));
                }
                return view;
            }
        };
        adapter1.setDropDownViewResource(R.layout.spinner_item_layout);
        adapter2.setDropDownViewResource(R.layout.spinner_item_layout);
        adapter3.setDropDownViewResource(R.layout.spinner_item_layout);
        spinner1.setAdapter(adapter1);
        spinner2.setAdapter(adapter2);
        spinner3.setAdapter(adapter3);
        spinner1.setSelection(0, true);
        spinner2.setSelection(0, true);
        spinner3.setSelection(0, true);
        spinner1.setOnItemSelectedListener(new BaseSpinner() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                loadingHorizontal = showLoadingHorizontal(getApplication(), "正在加载...").show();
                if (i == 0) {
                    category = "";
                } else if (i == 1) {
                    category = "0";
                } else if (i == 2) {
                    category = "1";
                } else if (i == 3) {
                    category = "2";
                }
                page = 1;
                mList.clear();
                mAdapter.notifyDataSetChanged();
                getMessagesData();
            }
        });
        spinner2.setOnItemSelectedListener(new BaseSpinner() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                loadingHorizontal = showLoadingHorizontal(getApplication(), "正在加载...").show();
                if (i == 0) {
                    procurementMode = "";
                } else if (i == 1) {
                    procurementMode = "0";
                } else if (i == 2) {
                    procurementMode = "1";
                }
                page = 1;
                mList.clear();
                mAdapter.notifyDataSetChanged();
                getMessagesData();
            }
        });
        spinner3.setOnItemSelectedListener(new BaseSpinner() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                loadingHorizontal = showLoadingHorizontal(getApplication(), "正在加载...").show();
                if (i == 0) {
                    moneyRange = "";
                } else if (i == 1) {
                    moneyRange = "0";
                } else if (i == 2) {
                    moneyRange = "1";
                } else if (i == 3) {
                    moneyRange = "2";
                }
                page = 1;
                mList.clear();
                mAdapter.notifyDataSetChanged();
                getMessagesData();
            }
        });
    }
}